package com.venutech.gongan.util;

import org.apache.commons.lang3.StringUtils;
import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.UUID;

public class BarcodeUtil2 {



    /**
     * 生成文件
     *
     * @param msg
     * @param path
     * @return
     */
    public static File generateFile(String msg, String path) {
        File file = new File(path);

        try {
            generate(msg, new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

        return file;
    }

    public static File generateFile(String msg, File filePath, String fileName) {
        File file = new File(filePath, fileName);

        try {
            generate(msg, new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

        return file;
    }


    //背景图上添加图片
    public static void step1(String backgroundImgPath,String contentImgPath) throws IOException{
        try {
            BufferedImage img = new BufferedImage(437, 176, BufferedImage.TYPE_INT_RGB);

            BufferedImage bg = ImageIO.read(new File(backgroundImgPath));

            BufferedImage content = ImageIO.read(new File(contentImgPath));

            //一维码背景色


            // 中间内容框画到背景图上
            Graphics g = img.getGraphics();
            g.drawImage(bg.getScaledInstance(437, 176, Image.SCALE_DEFAULT), 0, 0, null);
            g.drawImage(content.getScaledInstance(370, 120, Image.SCALE_DEFAULT), 30, 20, null);

            g.dispose();
//            ImageIO.write(img, "png", new File("d:\\test.png"));
            ImageIO.write(img, "png", new File(contentImgPath));
            System.out.println("生成成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void drawImage(byte[] bgImgData, String contentImgPath) throws IOException{
        try {
            BufferedImage img = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);

            //BufferedImage bg = ImageIO.read(new File(backgroundImgPath));
            ByteArrayInputStream in = new ByteArrayInputStream(bgImgData);
            BufferedImage bg = ImageIO.read(in);
            BufferedImage content = ImageIO.read(new File(contentImgPath));

            //一维码背景色


            // 中间内容框画到背景图上
            Graphics g = img.getGraphics();
            g.drawImage(bg.getScaledInstance(400, 300, Image.SCALE_DEFAULT), 0, 0, null);
            g.drawImage(content.getScaledInstance(300, 150, Image.SCALE_DEFAULT),50, 50, null);

            g.dispose();
//            ImageIO.write(img, "png", new File("d:\\test.png"));
            ImageIO.write(img, "png", new File(contentImgPath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成字节
     *
     * @param msg
     * @return
     */
    public static byte[] generate(String msg) {
        ByteArrayOutputStream ous = new ByteArrayOutputStream();
        generate(msg, ous);
        return ous.toByteArray();
    }

    /**
     * 生成到流
     *
     * @param msg
     * @param ous
     */
    public static void generate(String msg, OutputStream ous) {
        if (StringUtils.isEmpty(msg) || ous == null) {
            return;
        }

        Code39Bean bean = new Code39Bean();

        // 精细度
        final int dpi = 150;
        // module宽度
        final double moduleWidth = UnitConv.in2mm(1.0f / dpi);

        // 配置对象
        bean.setModuleWidth(moduleWidth);
        bean.setWideFactor(3);
        bean.doQuietZone(false);


        String format = "image/png";
        try {

            // 输出到流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi,
                    BufferedImage.TYPE_BYTE_BINARY, false, 0);

            // 生成条形码
            bean.generateBarcode(canvas, msg);

            // 结束绘制
            canvas.finish();



        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //接收条码录入的内容

    /**
     * 录入18位编码
     * @throws IOException
     */
   /* public static void inputMsg(String xzqy,String pcs,String dl,String xl,String cx,String xh) throws IOException {
        //条码内容
        String msg = xzqy+pcs+dl+xl+cx+xh;
        //条码生成路径
        String path = "src\\main\\resources\\static\\BarCode\\barcode.png";
        File contentImgPath= generateFile(msg, path);

        //背景图片
        File file = new File("D:\\bg2.png");
        String backgroundImgPath = file.getPath();
        String contentPath=contentImgPath.getPath();
        step1(backgroundImgPath,contentPath);
    }*/

    public static void inputMsg(String sn) throws IOException {
        //条码内容
        String msg = sn;
        String picId = UUID.randomUUID().toString()+sn;
        picId=picId.substring(picId.lastIndexOf('-')+1);
        //条码生成路径
        String path = "src\\main\\resources\\static\\BarCode\\"+picId+".png";
        File contentImgPath= generateFile(msg, path);

        //背景图片
        File file = new File("imd-web/src/main/resources/static/bg2.png");
        String backgroundImgPath = file.getPath();
        String contentPath=contentImgPath.getPath();
        step1(backgroundImgPath,contentPath);
    }

    public static void inputMsg2(String sn, File fileDir, String fileName, byte[] bgFileData) throws IOException {
        //条码内容
        String msg = sn;

        //条码生成路径
        //String path = String.format("%s\\%s", fileDir, fileName);
        File contentImgPath= generateFile(msg, fileDir, fileName);

        //背景图片
//        File file = new File("imd-web/src/main/resources/static/bg2.png");
//        String backgroundImgPath = file.getPath();
//        String contentPath=contentImgPath.getPath();
        drawImage(bgFileData,contentImgPath.getPath());

        //return picId;
    }

    public static void main(String[] args) throws IOException{

//        inputMsg("szga","01","01","01","1","58478");

    }
}
